package cn.lyh.edu.controller;

import cn.lyh.annotation.EnableSysLog;
import cn.lyh.base.BaseResult;
import cn.lyh.exception.BadRequestException;
import cn.lyh.edu.domain.EduLessonType;
import cn.lyh.edu.service.IEduLessonTypeService;
import cn.lyh.edu.service.dto.LessonTypeQueryCriteria;
import cn.lyh.utils.PageVo;
import cn.lyh.utils.ResultVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**功能描述：课程类型前端控制器
 */
@RestController
@RequestMapping("lessonType")
public class EduLessonTypeController {

    private final IEduLessonTypeService eduLessonTypeService;

    public EduLessonTypeController(IEduLessonTypeService eduLessonTypeService) {
        this.eduLessonTypeService = eduLessonTypeService;
    }

//    @EnableSysLog("获取课程类型列表数据")
    @GetMapping
    public ResponseEntity<Object> getList(LessonTypeQueryCriteria queryCriteria,
                                          PageVo pageVo){
        Pageable pageable = PageRequest.of(pageVo.getPageIndex()-1, pageVo.getPageSize(),
                Sort.Direction.DESC,"id");
        return new ResponseEntity<>(eduLessonTypeService.getList(queryCriteria,pageable), HttpStatus.OK);
    }

//    @EnableSysLog("新增课程类型信息")
    @PostMapping
    public BaseResult addLessonType(@RequestBody EduLessonType eduLessonType){
        eduLessonTypeService.addLessonType(eduLessonType);
        return BaseResult.success("添加成功");
    }

//    @EnableSysLog("根据ID获取课程类型的详情信息")
    @GetMapping("/{id}")
    public BaseResult detail(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("获取信息失败");
        }
        return BaseResult.success(eduLessonTypeService.getById(id));
    }

//    @EnableSysLog("更新课程类型信息")
    @PutMapping
    public BaseResult editLessonType(@RequestBody EduLessonType eduLessonType){
        eduLessonTypeService.editLessonType(eduLessonType);
        return BaseResult.success("更新成功");
    }

//    @EnableSysLog("删除课程类型信息")
    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("删除信息失败");
        }
        eduLessonTypeService.deleteById(id);
        return BaseResult.success("删除成功");
    }

//    @EnableSysLog("【后台】获取首页课程类型")
    @GetMapping("all")
    public BaseResult all(){
        List<EduLessonType> list = eduLessonTypeService.getAll();
        List<ResultVo> resultVoList = list.stream().map(temp-> {
           ResultVo obj = new ResultVo();
           obj.setName(temp.getTypeName());
           obj.setId(temp.getId());
           return obj;
        }).collect(Collectors.toList());

        return BaseResult.success(resultVoList);
    }

}
